<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    let maxSumTwoNoOverlap = function (nums, firstLen, secondLen) {
      let len = nums.length;
      let sum = new Array(len + 1).fill(0);
      for (let i = 0; i < len; i++) {
        sum[i + 1] = sum[i] + nums[i];
      }
      let res = 0;
      for (let i = 0; i <= len - firstLen; i++) {
        let first = sum[i + firstLen] - sum[i];
        for (let j = 0; j <= len - secondLen; j++) {
          if (j >= i + firstLen || j + secondLen <= i) {
            let second = sum[j + secondLen] - sum[j];
            res = Math.max(res, first + second);
          }
        }
      }
      return res;
    };
    // 思路
    // 1. 先求出前缀和数组
    // 2. 遍历数组，分别求出前缀和数组中的前firstLen项和后secondLen项的和
    console.log(maxSumTwoNoOverlap(nums = [0, 6, 5, 2, 2, 5, 1, 9, 4], firstLen = 1, secondLen = 2))
  </script>
</body>

</html>